package algotithm.leetcode.dichotomy.test81;

/**
 * @author zhouyanxiang
 * @Date 2021-03-2021/3/30-23:23
 * @Title 81. 搜索旋转排序数组 II
 */
public class Solution {

    public static void main(String[] args) {
        int[] nums = {3,5,1};
        int target = 1;
        Solution solution = new Solution();
        boolean search = solution.search(nums, target);
        System.out.println(search);
    }

    public boolean search(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        int n = nums.length;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] == target) {
                return true;
            }
            if (nums[l] == nums[mid]) {
                l++;
                continue;
            }
            if (nums[l] < nums[mid]) {
                if (nums[mid] > target && target >= nums[l]) {
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            } else {
                if (nums[mid] < target && target <= nums[r]) {
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            }
        }
        return false;
    }
}
